/*
   Copyright (C) 2009 Xizhi Zhu.  All Rights Reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
   3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __p2p_packet_h__
#define __p2p_packet_h__

#include <vector>
#include "adu.h"
#include "node.h"

// type of the packet
enum P2PPktType {PktTypeRegRequest, PktTypeRegReply,
                 PktTypeUnregRequest,
                 PktTypeNodeListRequest, PktTypeNodeListReply,
                 PktTypeChunkListRequest, PktTypeChunkListReply,
                 PktTypeChunkRequest, PktTypeChunkReply
                };

// packets used
class P2PPacket: public ADU
{
public:
    P2PPacket(P2PPktType type);

    // the size is returned according to the specific type of the packet
    virtual int size() const;

    // data field

    // in common use
    P2PPktType type_;
    nsaddr_t id_;
    double timestamp_;

    // reg/unreg request
    bool isServer_;

    // reg reply

    // node list request

    // node list reply
    std::vector<nsaddr_t> server_list_;
    std::vector<nsaddr_t> peer_list_;

    // chunk request
    int chunk_request_; // the chunk requested

    // chunk reply
    int chunk_reply_;   // the chunk replied
                        // -1 means it doesn't want to reply

    // chunk list request

    // chunk list reply
    std::vector<int> available_chunks_;
};

#endif // __p2p_packet_h__

